<template>
  <div class="app-container">
    <!--==========================================================================================================================================================================================================================================-->
    <el-form :model="formData" ref="formData" label-width="80px">
      <div class="box">
        <div class="box_content">
          <el-form-item label="学期">
            <el-select v-model="formData.SelectSemester" placeholder="全部">
              <el-option v-for="item in semester" :key="item.value" :label="item.label" :value="item.label"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="box_content">
          <el-form-item label="课程搜索">
            <el-select v-model="formData.SelectCourse" placeholder="全部">
              <el-option v-for="item in course" :key="item.value" :label="item.label" :value="item.label"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="box_content">
          <el-form-item label="副跟进人">
            <el-select v-model="formData.SelectTeacher" placeholder="全部">
              <el-option v-for="item in teacher" :key="item.value" :label="item.label" :value="item.label"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="box_content">
          <el-form-item label="搜索">
            <el-input placeholder="关键字搜索" v-model="formData.search" class="input-with-select">
              <el-select v-model="formData.type" slot="prepend" placeholder="请选择">
                <el-option label="学员状态" value="学员状态"></el-option>
                <el-option label="学生姓名" value="学生姓名"></el-option>
                <el-option label="以往就读学校" value="以往就读学校"></el-option>
                <el-option label="联系方式" value="联系方式"></el-option>
                <el-option label="主跟进人" value="主跟进人"></el-option>
                <el-option label="副跟进人" value="副跟进人"></el-option>
                <el-option label="来源途径" value="来源途径"></el-option>
              </el-select>
            </el-input>
          </el-form-item>
        </div>
        <div class="box_content">
          <el-form-item label="日期">
            <div class="block">
              <el-date-picker v-model="formData.time" type="daterange" range-separator="至" start-placeholder="开始日期"
                              end-placeholder="结束日期"></el-date-picker>
            </div>
          </el-form-item>
        </div>
        <div class="box_content">
          <div style="text-align: left;margin-bottom: 10px">
            <el-button type="primary" icon="el-icon-search">查询</el-button>
            <el-button icon="el-icon-search">重置</el-button>
          </div>
        </div>
      </div>
    </el-form>
    <!--==========================================================================================================================================================================================================================================-->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="newFile" class="btn">新建教材</el-button>
        <el-button class="btn">导入教材</el-button>
        <el-button class="btn">导出教材</el-button>
      </el-col>
      <el-col :span="12" style="text-align: right">
        <el-button class="btn" type="primary" @click="home">主页</el-button>
        <el-button class="btn" type="danger" @click="delList">回收站</el-button>
      </el-col>
    </el-row>
    <!--==========================================================================================================================================================================================================================================-->
   <el-table v-loading="loading" :data="queryData" border style="width: 100%" tooltip-effect="dark" @selection-change="handleSelectionChange">
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="textbook_name" label="教材名称" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="textbook_house" label="出版社" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="public_date" label="出版时间" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="section[0].num" label="章节" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="section[0].num-2" label="子章节" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="" label="目录" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="know[0].num" label="知识点" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="inventory.inventory" label="库存" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="createor_name" label="创建人" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="create_time" label="创建时间" :show-overflow-tooltip="true"></el-table-column>
     <el-table-column prop="update_time" label="最后修改时间" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column fixed="right" label="操作" width="275">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="primary" size="mini">查看</el-button>
          <el-button type="success" size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button type="info" size="mini" @click="addUp(scope.row)">入库</el-button>
          <el-button type="danger" size="mini" @click="delData(scope.row)" v-show="pagecheck">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <span class="demonstration"></span>
      <div class="block">
        <span class="demonstration"></span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog v-loading="loading" title="新建教材:" :visible.sync="NewteachingMaterial" width="100%">
      <el-row>
        {{addData}}
        <el-col :span="11" class="app-container"
                style="border: 1px solid #DCDFE6;margin-right: 10px;border-radius: 25px;height: 786px;">
          <div>教材名称：{{addData.textbook_name}}</div>
          <div>教材出版社：{{addData.textbook_house}}</div>
          <div>出版日期：{{addData.public_date}}</div>
          <h4 style="font-weight: bold">章节内容：</h4>
          <div></div>
          <el-tree :data="tree" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="true"></el-tree>
        </el-col>
        <el-col :span="12" class="app-container" style="border: 1px solid #DCDFE6;border-radius: 25px;">
            <el-form :rules="rules" ref="addData" :model="addData" label-width="100px" class="demo-ruleForm">
              <el-form-item label="教材名称:" prop="textbook_name">
                <el-input v-model="addData.textbook_name" placeholder="请输入教材名称"></el-input>
              </el-form-item>
              <el-form-item label="教材出版社:" prop="textbook_house">
                <el-input v-model="addData.textbook_house" placeholder="请输入教材出版社"></el-input>
              </el-form-item>
              <el-form-item label="年级:" prop="grade">
                <el-select v-model="addData.grade" value="" placeholder="请选择">
                  <el-option v-for=" o in 12" :key="o" :label="o" :value="o"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="出版日期:" prop="public_date">
                <div class="block">
                  <el-date-picker v-model="addData.public_date" format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
                </div>
              </el-form-item>
              <el-form-item label="章节内容:">

              </el-form-item>
              <el-col :span="12">
                <el-form-item label="章节:">
                  <el-input v-model="addData.section[0].section_name" placeholder="章节"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="标题:">
                  <el-input v-model="addData.section[0].child[0].section_name" placeholder="标题"></el-input>
                </el-form-item>
              </el-col>
              <div v-show="showunit" v-for="item in unit" style="text-align: center">
                <el-col :span="24">
                  <el-form-item label="单元:">
                    <el-input v-model="addData.section[0].child[0].child[item-1].section_name" placeholder="单元"></el-input>
                  </el-form-item>
                </el-col>
              </div>
            </el-form>
          <el-form label-width="50px">
            <el-button type="primary" @click="addunit" v-show="showunit">+添加单元</el-button>
            <el-button @click="addClick('addData')" type="success" style="margin-bottom: 20px" v-show="showunit">保存教材</el-button>
            <el-button v-show="showunit" type="danger" @click="rest">重置</el-button>
            <div v-show="saveKnowShow">
              <el-form-item label="单元">
                <el-select v-model="unitname" value="" placeholder="请选择">
                  <el-option v-for="item in selectunit" :value="item" :label="item"></el-option>
                </el-select>
              </el-form-item>
            </div>
            <div v-for="it in knows.know_data.length/2" v-show="saveKnowShow">
              <el-col :span="12">
                <div v-if="it>inputLength/2&&inputLength!=0">
                  <div v-if="it==1">
                    <el-form-item label="知识点:" label-width="75px">
                      <el-input v-model="knows.know_data[it-1].point_name" placeholder="知识点"></el-input>
                    </el-form-item>
                  </div>
                  <div v-if="it==2">
                    <el-form-item label="知识点:" label-width="75px">
                      <el-input v-model="knows.know_data[it].point_name" placeholder="知识点"></el-input>
                    </el-form-item>
                  </div>
                  <div v-if="it!=0&&it>2">
                    <el-form-item label="知识点:" label-width="75px">
                      <el-input v-model="knows.know_data[it+it-2].point_name" placeholder="知识点"></el-input>
                    </el-form-item>
                  </div>
                </div>
              </el-col>
              <el-col :span="12">
                <div v-if="it>inputLength/2&&inputLength!=0">
                  <div v-if="it==1">
                    <el-form-item label="考点:" label-width="75px">
                      <el-input v-model="knows.know_data[it].point_name" placeholder="考点"></el-input>
                    </el-form-item>
                  </div>
                  <div v-if="it!=0&&it>1">
                    <el-form-item label="考点:" label-width="75px">
                      <el-input v-model="knows.know_data[it+it-1].point_name" placeholder="考点"></el-input>
                    </el-form-item>
                  </div>
                </div>
              </el-col>
            </div>
            <el-col :span="24">
              <el-button v-show="saveKnowShow" type="primary" @click="methodName" style="margin-bottom: 20px">+添加知识点/考点</el-button>
              <el-button v-show="saveKnowShow" type="success" @click="saveKnow">保存知识点/考点</el-button>
              <el-button v-show="saveKnowShow" type="danger" @click="rest">重置</el-button>
            </el-col>
          </el-form>
        </el-col>
        <!--<el-button type="primary" @click="addExamPoint">+添加考点</el-button>-->
      </el-row>
    </el-dialog>
    <el-dialog v-loading="loading" title="库存数量:" :visible.sync="Warehousing" width="600px">
      <div>
        <el-form label-width="120px">
          <el-form-item label="原库存数量：">
            <div>{{addUpdata.oldNum}}</div>
          </el-form-item>
          <el-form-item label="新增库存数量：">
            <el-input v-model="addUpdata.addNum" @blur="addNum" :disabled="addNumDis"></el-input>
          </el-form-item>
          <el-form-item label="修正库存数量：">
            <el-input v-model="addUpdata.editNum" @blur="editNum" :disabled="editNumDis"></el-input>
          </el-form-item>
          <el-form-item label="备注：">
            <el-input v-model="addUpdata.info" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="操作人：">
            <div>{{username}}</div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addUpdate">确认存入/修改</el-button>
            <el-button type="danger" @click="rest">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <el-dialog v-loading="loading" title="查看" :visible.sync="query" width="600px">
      <el-row>
        <div class="queryTitle">教材名称：{{queryInfo.textbook_name}}</div>
        <div class="queryTitle">教材出版社 ： {{queryInfo.textbook_house}}</div>
        <div class="queryTitle">出版日期：{{queryInfo.public_date}}</div>
        <div class="queryTitle">章节内容：</div>
      </el-row>
      <el-row>
        <el-col :span="24" style="margin-top: 20px;margin-bottom: 20px;border: 1px solid #DCDFE6;border-radius: 25px" class="app-container">
          <el-tree :data="tree" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="true"></el-tree>
        </el-col>
      </el-row>
      <el-row>
        <div>历史记录：</div>
        <div>1、{{queryInfo.create_time}}，由{{queryInfo.createor_name}}创建</div>
        <div>2、2020-10-10 23:40:07，有曾小贤添加库存，数量为：100</div>
        <div>3、2020-10-11 12:40:07，有曾小贤修改 知识点“BE动词”为“Be动词”</div>
      </el-row>
    </el-dialog>
    <el-dialog v-loading="loading" title="编辑:" :visible.sync="editDig" width="600px">
      <el-row>
        <el-col :span="24" class="app-container" style="border: 1px solid #DCDFE6;border-radius: 25px;">
          <el-form :rules="rules" ref="addData" :model="addData" label-width="100px" class="demo-ruleForm">
            <el-form-item label="教材名称:">
              <el-input v-model="addData.textbook_name" placeholder="请输入教材名称"></el-input>
            </el-form-item>
            <el-form-item label="教材出版社:">
              <el-input v-model="addData.textbook_house" placeholder="请输入教材出版社"></el-input>
            </el-form-item>
            <el-form-item label="年级:">
              <el-select v-model="addData.grade" value="" placeholder="请选择">
                <el-option v-for=" o in 12" :key="o" :label="o" :value="o"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="出版日期:">
              <div class="block">
                <el-date-picker v-model="addData.public_date" format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
              </div>
            </el-form-item>
            <el-form-item label="章节内容:">

            </el-form-item>
            <el-col :span="12">
              <el-form-item label="章节:">
                <el-input v-model="addData.section[0].section_name" placeholder="章节"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="标题:">
                <el-input v-model="addData.section[0].child[0].section_name" placeholder="标题"></el-input>
              </el-form-item>
            </el-col>
            <div v-if="editLength!=0">
              <div v-for="item in editLength-2" style="text-align: center">
                {{editData.section[item+1].unid}}
                <el-col :span="24">
                  <el-form-item label="单元:" label-width="60px">
                    <el-input v-model="addData.section[0].child[0].child[item-1].section_name" placeholder="单元"></el-input>
                  </el-form-item>
                </el-col>
                <div v-if="editData!=null||editData.know!=null||knows.know_data!=null">
                  <div v-for="item2 in knows.know_data.length">
                    <div v-if="editData.section[item+1].unid == knows.know_data[item2-1].section_id">
                      <el-col :span="12" v-if="(item2-1)%2==0">
                        <el-form-item label="知识点:" label-width="100px">
                          <el-input v-model="knows.know_data[item2-1].point_name" placeholder="单元"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12" v-if="(item2-1)%2!=0">
                        <el-form-item label="考点:" label-width="100px">
                          <el-input v-model="knows.know_data[item2-1].point_name" placeholder="单元"></el-input>
                        </el-form-item>
                      </el-col>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div v-show="showunit" v-for="item in unit" style="text-align: center">
              <el-col :span="24">
                <el-form-item label="单元:">
                  <el-input v-model="addData.section[0].child[0].child[item-1+editLength-2].section_name" placeholder="单元"></el-input>
                </el-form-item>
              </el-col>
            </div>
          </el-form>
          <el-form label-width="50px">
            <el-button type="primary" @click="addunit" v-show="showunit">+添加单元</el-button>
            <el-button @click="editD" type="success" style="margin-bottom: 20px" v-show="showunit">保存教材</el-button>
            <el-button v-show="showunit" type="danger" @click="rest">重置</el-button>
            <div v-show="saveKnowShow">
              <el-form-item label="单元">
                <el-select v-model="unitname" value="" placeholder="请选择">
                  <el-option v-for="item in selectunit" :value="item" :label="item"></el-option>
                </el-select>
              </el-form-item>
            </div>
            <div v-for="it in knows.know_data.length/2" v-show="saveKnowShow">
              <!--{{editknowLength/2}}
              {{it-1}}____{{knows.know_data.length/2}}-->
              <div v-if="(it-1)>=editknowLength/2">
                <el-col :span="12">
                  <div v-if="it>inputLength/2&&inputLength!=0">
                    <div v-if="it==1">
                      <el-form-item label="知识点:" label-width="75px">
                        <el-input v-model="knows.know_data[it-1].point_name" placeholder="知识点"></el-input>
                      </el-form-item>
                    </div>
                    <div v-if="it==2">
                      <el-form-item label="知识点:" label-width="75px">
                        <el-input v-model="knows.know_data[it].point_name" placeholder="知识点"></el-input>
                      </el-form-item>
                    </div>
                    <div v-if="it!=0&&it>2">
                      <el-form-item label="知识点:" label-width="75px">
                        <el-input v-model="knows.know_data[it+it-2].point_name" placeholder="知识点"></el-input>
                      </el-form-item>
                    </div>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div v-if="it>inputLength/2&&inputLength!=0">
                    <div v-if="it==1">
                      <el-form-item label="考点:" label-width="75px">
                        <el-input v-model="knows.know_data[it].point_name" placeholder="考点"></el-input>
                      </el-form-item>
                    </div>
                    <div v-if="it!=0&&it>1">
                      <el-form-item label="考点:" label-width="75px">
                        <el-input v-model="knows.know_data[it+it-1].point_name" placeholder="考点"></el-input>
                      </el-form-item>
                    </div>
                  </div>
                </el-col>
              </div>
            </div>
            <el-col :span="24">
              <el-button v-show="saveKnowShow" type="primary" @click="methodName" style="margin-bottom: 20px">+添加知识点/考点</el-button>
              <el-button v-show="saveKnowShow" type="success" @click="editKnow">保存知识点/考点</el-button>
              <el-button v-show="saveKnowShow" type="danger" @click="rest">重置</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-row>
    </el-dialog>
    <!--==========================================================================================================================================================================================================================================-->
  </div>
</template>

<script>
  import { addTeach,addKnowledge,queryTeach,queryTeachById,addUpdate,delTeach } from '@/api/studentRecruit.js'
  import { getSessionInfo, delSessionInfo } from '@/utils/auth'
  import store from '../../store' // get token
  export default {
    name: 'index',
    data() {
      return {
        editknowLength:0,
        editSucc:null,
        editLength:0,
        editShow:true,
        editData:{},
        editDig:false,
        delstatus:0,
        selstatus:0,
        inputLength:1,
        knowArr:[],
        saveKnowShow:false,
        saveKnowData:null,
        unitname:null,
        selectunit:[],
        showunit:true,
        knowIndex:0,
        objKnow:{},
        unit:0,
        knowPoint:[],
        loading:null,
        oldNum:'0',
        total:0,
        page:1,
        limit:10,
        knowpoint:1,
        exampoint:1,
        querySection:null,
        addNumDis:false,
        editNumDis:false,
        oldnumb:null,
        username:null,
        currentPage:1,
        queryInfo:[],
        queryData:null,
        num1:0,
        num2:1,
        teachId:null,
        addUpdata:{
          addNum:null,
          editNum:null,
          oldNum:null,
          info:null,
        },
        rules: {
          textbook_name: [
            { required: true, message: '请输入教材名称', trigger: 'blur,change' }
          ],
          textbook_house: [
            { required: true, message: '请输入出版社', trigger: 'blur,change' }
          ],
          grade: [
            { required: true, message: '请输入年级', trigger: 'blur,change' }
          ],
          public_date: [
            { type: 'date',required: true, message: '请选中日期', trigger: 'blur,change' }
          ],
          chapter: [
            { required: true, message: '请输入章节', trigger: 'blur,change' }
          ],
          title: [
            { required: true, message: '请输入标题', trigger: 'blur,change' }
          ],
          unit: [
            { required: true, message: '请输入单元', trigger: 'blur,change' }
          ]
        },
        addData:{
          unid:null,
          textbook_name:null, //教材名称：
          textbook_house:null,  //教材出版社 ：
          public_date:null, //出版日期：
          grade:null, //年级
          section:[{
            section_name:null,  //Unit 16
            level:1,
            child:[{
              section_name:null,  //On the london Eye
              level:2,
              child:[]
            }]
          }],
        },
        knows:{
          know_data:[],
          textbook_id:null
        },
        formData: {
          SelectSemester: '',
          SelectCourse: '',
          SelectTeacher: '',
          search: '',
          type: '',
          time: ''
        },
        tree: [{
          label: null,
          children: [{
            label: null,
            children: [/*{
              label: 'Lesson 31',
              children: [{
                label: '知识点：',
                children: [{
                  label: 'Be动词'
                }, {
                  label: 'Be动词+地点'
                }]
              }, {
                label: '考点：',
                children: [{
                  label: '自我介绍'
                }, {
                  label: '主语人称代词'
                }]
              }]
            }*//*, {
              label: 'Lesson 32',
              children: [{
                label: '知识点：',
                children: [{
                  label: '现在进行时'
                }, {
                  label: '形容词性物主代词'
                }]
              }, {
                label: '考点：',
                children: [{
                  label: '简短回答'
                }]
              }]
            }*/]
          }]
        }],
        tree2: [{
          label: 'Unit 16',
          children: [{
            label: 'On the london Eye',
            children: [{
              label: 'Lesson 31',
              children: [{
                label: '知识点：',
                children: [{
                  label: 'Be动词'
                }, {
                  label: 'Be动词+地点'
                }]
              }, {
                label: '考点：',
                children: [{
                  label: '自我介绍'
                }, {
                  label: '主语人称代词'
                }]
              }]
            }, {
              label: 'Lesson 32',
              children: [{
                label: '知识点：',
                children: [{
                  label: '现在进行时'
                }, {
                  label: '形容词性物主代词'
                }]
              }, {
                label: '考点：',
                children: [{
                  label: '简短回答'
                }]
              }]
            }]
          }]
        }],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        NewteachingMaterial: false,
        Warehousing: false,
        query: false,
        //教师
        teacher: [{
          value: '选项1',
          label: '张老师'
        }, {
          value: '选项2',
          label: '李老师'
        }, {
          value: '选项3',
          label: '黄老师'
        }],
        //课程
        course: [{
          value: '选项1',
          label: '语文'
        }, {
          value: '选项2',
          label: '英语'
        }, {
          value: '选项3',
          label: '数学'
        }],
        //学期
        semester: [{
          value: '选项1',
          label: '第一学期'
        }, {
          value: '选项2',
          label: '第二学期'
        }]
      }
    },
    methods: {
      //编辑知识点
      async editKnow(){
        console.log('编辑教材返回数据',this.editSucc)
        console.log('单元名字',this.unitname)
        console.log('section_id',this.editSucc.section_id[this.unitname])
        this.knows.textbook_id = this.editSucc.textbook_id
        for (let i = this.editknowLength; i < this.knows.know_data.length; i++) {
          this.knows.know_data[i].textbook_id = this.editSucc.textbook_id
          this.knows.know_data[i].section_id = this.editSucc.section_id[this.unitname]
        }
        console.log(this.knows)
        let resData = await addKnowledge(this.knows)
        if(resData.code == 200){
          console.log('编辑知识点状态',resData)
          this.$message.success('编辑知识点/考点成功')
          this.editknowLength = this.knows.know_data.length
          this.queryTeach()
        }else {
          this.$message.error('编辑失败')
        }
      },
      //编辑修改教材
      async editD(){
        this.selectunit = []
        this.editShow = true
        let delayTime = new Date(this.addData.public_date).toJSON().split('T')[0];
        this.addData.public_date = delayTime
        console.log('要编辑的数据',this.addData)
        let resData = await addTeach(this.addData)
        if(resData.code == 200){
          console.log('编辑成功',resData)
          this.editSucc = resData.data
          this.$message.success('编辑教材成功')
          this.queryTeach()
          this.saveKnowShow = true
          this.showunit = false
          for (let i = 0; i < this.addData.section[0].child[0].child.length; i++) {
            this.selectunit.push(this.addData.section[0].child[0].child[i].section_name)
          }
        }else {
          this.$message.error('编辑教材失败')
        }
        if (this.knows.know_data!=null){
          console.log('knows',this.knows)
          let resData2 = await addKnowledge(this.knows)
          if(resData2.code == 200){
            this.$message.success('编辑知识点/考点成功！')
          }else {
            this.$message.error('编辑知识点/考点失败！')
          }
          console.log('resData2',resData2)
        }
        console.log('修改的数据',this.addData)
        console.log('编辑状态',resData)
      },
      //编辑
      async edit(row){
        this.saveKnowShow = false
        this.showunit = true
        this.unit = 0
        this.editLength = 0
        this.addData.section[0].child[0].child = []
        this.editDig = true
        let queryData = {
          unid:row.unid
        }
        let resData = await queryTeachById(queryData)
        console.log(resData)
        this.editData = resData.data
        this.addData.unid = resData.data.unid
        this.addData.textbook_name = resData.data.textbook_name
        this.addData.textbook_house = resData.data.textbook_house
        this.addData.grade = resData.data.grade
        this.addData.public_date = resData.data.public_date
        this.addData.section[0].section_name = resData.data.section[0].section_name
        this.addData.section[0].child[0].section_name = resData.data.section[1].section_name
        if (resData.data.section.length!=0){
          for (let i = 2; i < resData.data.section.length; i++) {
            this.addData.section[0].child[0].child.push({ "section_name": resData.data.section[i].section_name, "level": 3 })
          }
        }
        if (resData.data.know.length!=0){
          this.knows.know_data = []
          this.knows.textbook_id = resData.data.unid
          for (let i = 0; i < resData.data.know.length; i++) {
            this.knows.know_data.push({'section_id':resData.data.know[i].section_id,'point_name':resData.data.know[i].point_name,'textbook_id':resData.data.know[i].textbook_id,'type':resData.data.know[i].type})
          }
          console.log('knows_data',this.knows)
          this.editknowLength = this.knows.know_data.length
        }
        console.log(this.addData.section[0].child[0].child)
        console.log('addData',this.addData)
        this.editLength = resData.data.section.length
        console.log('resData',resData.data)
      },
      //主页
      home(){
        this.page = 1
        this.queryTeach()
      },
      //回收站
      async delList(){
        this.delstatus = 1
        this.selstatus = 0
        this.loading = true
        this.queryData = []
        let queryData = {
          page:this.page,
          limit:this.limit,
          is_delete: 1
        }
        let resData = await queryTeach(queryData)
        console.log(resData)
        if (resData.code == 200){
          if(resData.data.list.length!=0){
            this.queryData = resData.data.list
            this.total = resData.data.total
            this.$message.success('查询成功！')
            this.loading = false
          }
        }else {
          this.$message.success('查询失败！')
        }
      },
      //删除教材
      async delData(row){
        console.log('教材ID',row)
        let delData = {
          unid:row.unid
        }
        let resData = await delTeach(delData)
        if(resData.code == 200){
          this.$message.success('删除成功！')
          this.queryTeach()
        }else {
          this.$message.error('删除失败！')
        }
      },
      newFile(){
        this.NewteachingMaterial = true
        this.tree[0].label = null
        this.tree[0].children[0].label = null
        for (let i = 0; i < this.tree[0].children[0].children.length; i++) {
          this.tree[0].children[0].children[i].label = null
          for (let j = 0; j < this.tree[0].children[0].children[i].children.length; j++) {
            this.tree[0].children[0].children[i].children[j].label = null
            for (let k = 0; k <  this.tree[0].children[0].children[i].children[j].children.length; k++) {
              this.tree[0].children[0].children[i].children[j].children[k].label = null
            }
          }
        }
        this.tree[0].children[0].children = []
      },
      async saveKnow(){
        this.loading = true
        console.log('教材添加成功返回数据',this.saveKnowData)
        console.log('单元名字',this.unitname)
        console.log('unitname',this.saveKnowData.data.section_id[this.unitname])
        if (this.inputLength == 1){
          for (let i = 0; i < this.knows.know_data.length; i++) {
            this.knows.know_data[i].section_id = this.saveKnowData.data.section_id[this.unitname]
            this.knows.know_data[i].textbook_id = this.saveKnowData.data.textbook_id
            if (i%2==0){
              this.knows.know_data[i].type = 1
            }else {
              this.knows.know_data[i].type = 2
            }
            this.knows.textbook_id = this.saveKnowData.data.textbook_id
          }
        }
        if (this.inputLength!=1){
          for (let i = this.inputLength; i < this.knows.know_data.length; i++) {
            this.knows.know_data[i].section_id = this.saveKnowData.data.section_id[this.unitname]
            this.knows.know_data[i].textbook_id = this.saveKnowData.data.textbook_id
            if (i%2==0){
              this.knows.know_data[i].type = 1
            }else {
              this.knows.know_data[i].type = 2
            }
            this.knows.textbook_id = this.saveKnowData.data.textbook_id
          }
        }
        console.log('添加数据',this.knows)
        let resData = await addKnowledge(this.knows)
        console.log('添加状态',resData)
        if (resData.code == 200){
          this.$message.success('保存成功')
          this.inputLength = this.knows.know_data.length
          let query = {
            unid:this.saveKnowData.data.textbook_id
          }
          let queryData = await queryTeachById(query)
          if (queryData.code == 200){
            this.$message.success('查看成功！')
            this.loading = false
            console.log(queryData)
            this.tree[0].label = queryData.data.section[0].section_name
            this.tree[0].children[0].label = queryData.data.section[1].section_name
            this.tree[0].children[0].children = []
            this.numArr1 = []
            for (let i = 2; i < queryData.data.section.length; i++) {
              console.log('标题unid',queryData.data.section[i].unid)
              this.numArr1.push(queryData.data.section[i])
            }
            this.numArr2 = []
            for (let j = 0; j < queryData.data.know.length; j++) {
              console.log('知识点unid',queryData.data.know[j].section_id)
              this.numArr2.push(queryData.data.know[j])
            }
            console.log('arr1',this.numArr1)
            console.log('arr2',this.numArr2)
            this.numArr3 = []
            for (let j = 0; j < this.numArr2.length; j++) {
              for (let i = 0; i < this.numArr1.length; i++) {
                if (this.numArr1[i].unid == this.numArr2[j].section_id){
                  this.numArr3.push({
                    unid:this.numArr1[i].unid,
                    sectionName:this.numArr1[i].section_name,
                    knowName:this.numArr2[j].point_name,
                    type:this.numArr2[j].type
                  })
                }
              }
            }
            for (let i = 2; i < queryData.data.section.length; i++) {
              this.tree[0].children[0].children.push({ label: null, children: [{ label: '知识点', children: [] },{ label: '考点', children: [] }] })
              this.tree[0].children[0].children[i-2].label = queryData.data.section[i].section_name
              console.log(this.tree[0].children[0].children[i-2].children[0].children)
              console.log(this.tree[0].children[0].children[i-2].children[1].label)
              for (let j = 0; j < this.numArr3.length; j++) {
                if (this.tree[0].children[0].children[i-2].label == this.numArr3[j].sectionName){
                  if (this.numArr3[j].type == 1){
                    this.tree[0].children[0].children[i-2].children[0].children.push({label: this.numArr3[j].knowName})
                  }else {
                    this.tree[0].children[0].children[i-2].children[1].children.push({label: this.numArr3[j].knowName})
                  }
                }
              }
            }
            console.log('arr3',this.numArr3)
            this.loading = false
          }else {
            this.$message.error('查看失败/查看出错！！')
            this.loading = false
          }
        }else {
          this.$message.error('保存失败')
        }
      },
      methodName(){
        this.knows.know_data.push({ section_id:null, point_name:null, textbook_id:null, type:1},{ section_id:null, point_name:null, textbook_id:null, type:2})
        this.tree[0].children[0].children.push({ label: null, children: []})
        /*for (let i = 0; i < this.tree[0].children[0].children.length; i++) {
          console.log(i)
          console.log('单元',this.tree[0].children[0].children)
          /!*console.log('label',this.tree[0].children[0].children[0].children[i].label)*!/
        }*/
        /*{
          label: '知识点：',
            children: [{
          label: 'Be动词'
        }, {
          label: 'Be动词+地点'
        }*/
        console.log('知识点长度',this.knows.know_data.length)
      },
      addunit(){
        this.addData.section[0].child[0].child.push({section_name:null,  level:3})
        this.tree[0].children[0].children.push({ label: null, children: [{ label: '知识点：', children: []},{ label: '考点：', children: []}]})
        /*this.unit = this.addData.section[0].child[0].child.length*/
        this.unit++
        console.log('添加单元',this.addData.section[0].child[0].child)
        console.log('单元下标',this.unit)
      },
      rest(){
        this.showunit = true
        this.saveKnowShow = false
        this.addData.textbook_name = null
        this.addData.textbook_house = null
        this.addData.textbook_grade = null
        this.addData.public_date = null
        this.addData.grade = null
        this.selectunit = []
        this.addData.section[0].section_name = null
        this.addData.section[0].child[0].section_name = null
        for (let i = 0; i < this.unit; i++) {
          this.addData.section[0].child[0].child[i].section_name = null
        }
        this.addData.section[0].child[0].child = []
        this.unit = 0
        this.tree[0].label = null
        this.tree[0].children[0].label = null
        this.tree[0].children[0].children = []
        this.$message.success('重置成功')
      },
      addNum(){
        if (this.addUpdata.addNum != null){
          this.editNumDis = true
        }else if(this.addUpdata.addNum == null){
          this.editNumDis = false
        }
      },
      editNum(){
        if (this.addUpdata.editNum != null){
          this.addNumDis = true
        }else if (this.addUpdata.editNum == null){
          this.addNumDis = false
        }
      },
      addUp(row){
        this.Warehousing = true
        console.log(row.unid)
        this.teachId = row.unid
        console.log(this.addUpdata)
        console.log('库存数量',row.inventory.inventory)
        this.addUpdata.oldNum = row.inventory.inventory
        console.log('教材ID',row.inventory.textbook_id)
        const hasToken = JSON.parse(getSessionInfo())
        console.log('用户名',hasToken.username)
        this.username = hasToken.username
      },
      //修改库存
      async addUpdate(){
        this.loading = true
        let updataData = {
          unid:this.teachId,
          addNum:this.addUpdata.addNum,
          editNum:this.addUpdata.editNum,
          oldNum:this.addUpdata.oldNum,
          info:this.addUpdata.info,
        }
        console.log(updataData)
        let resData = await addUpdate(updataData)
        if(resData.code == 200){
          this.$message.success("入库成功！")
          this.loading = false
          this.Warehousing = false
          this.queryTeach()
        }else {
          this.$message.error("入库失败！")
          this.loading = false
        }
      },
      //分页全查询
      async queryTeach(){
        this.pagecheck = true
        this.delstatus = 0
        this.selstatus = 1
        this.loading = true
        let queryData = {
          page:this.page,
          limit:this.limit,
          is_delete:0
        }
        let resData = await queryTeach(queryData)
        if (resData.code == 200){
          this.total = resData.data.total
          this.queryData = resData.data.list
          console.log('主页全查询',resData.data.list)
          this.loading = false
        }
        this.NewteachingMaterial = false
        /*let child = { section_name:null,level:3 }*/
        /*console.log(this.addData.section[0].child[0].child[0].know_data)*/
        /*this.$set(this.addData.section[0].child[0].child[0],'child',child)*/
        /*this.$set(this.addData.section[0].child[0],'child',111)
        console.log(this.addData.section[0].child[0])*/
      },
      addClick(addData) {
        this.$refs[addData].validate((valid) => {
          if (valid) {
            this.saveKnowShow = true
            this.showunit = false
            this.loading = true
            let delayTime = new Date(this.addData.public_date).toJSON().split('T')[0];
            this.addData.public_date = delayTime
            addTeach(this.addData).then(resData => {
              if(resData.code == 200){
                console.log(resData.data.section_id)
                this.$message.success('添加教材成功!');
                this.loading = false
                this.tree[0].label = this.addData.section[0].section_name
                this.tree[0].children[0].label = this.addData.section[0].child[0].section_name
                console.log('下标',this.tree[0].children[0].children.length)
                for (let i = 0; i < this.tree[0].children[0].children.length; i++) {
                  console.log(this.addData.section[0].child[0].child[i].section_name)
                  this.tree[0].children[0].children[i].label = this.addData.section[0].child[0].child[i].section_name
                }
                for (let i = 0; i < this.addData.section[0].child[0].child.length; i++) {
                  this.selectunit.push(this.addData.section[0].child[0].child[i].section_name)
                }
                this.saveKnowData = resData
              }else {
                this.$message.error('添加失败！');
              }
            })
          } else {
            console.log(valid);
            return false;
          }
        });
      },
      async handleClick(row) {
        this.loading = true
        this.query = true
        console.log('章节ID',row)
        let query = {
          unid:row.unid
        }
        let queryData = await queryTeachById(query)
        if (queryData.code == 200){
          this.$message.success('查看成功！')
          this.loading = false
          console.log(queryData)
          this.tree[0].label = queryData.data.section[0].section_name
          this.tree[0].children[0].label = queryData.data.section[1].section_name
          this.tree[0].children[0].children = []
          this.numArr1 = []
          for (let i = 2; i < queryData.data.section.length; i++) {
            /*console.log('知识点',this.tree[0].children[0].children[i-2].children[0])
            console.log('考点',this.tree[0].children[0].children[i-2].children[1])*/
            console.log('标题unid',queryData.data.section[i].unid)
            this.numArr1.push(queryData.data.section[i])
          }
          this.numArr2 = []
          for (let j = 0; j < queryData.data.know.length; j++) {
            console.log('知识点unid',queryData.data.know[j].section_id)
            this.numArr2.push(queryData.data.know[j])
          }
          console.log('arr1',this.numArr1)
          console.log('arr2',this.numArr2)
          this.numArr3 = []
          for (let j = 0; j < this.numArr2.length; j++) {
            for (let i = 0; i < this.numArr1.length; i++) {
              if (this.numArr1[i].unid == this.numArr2[j].section_id){
                this.numArr3.push({
                  unid:this.numArr1[i].unid,
                  sectionName:this.numArr1[i].section_name,
                  knowName:this.numArr2[j].point_name,
                  type:this.numArr2[j].type
                })
              }
            }
          }
          for (let i = 2; i < queryData.data.section.length; i++) {
            this.tree[0].children[0].children.push({ label: null, children: [{ label: '知识点', children: [] },{ label: '考点', children: [] }] })
            this.tree[0].children[0].children[i-2].label = queryData.data.section[i].section_name
            console.log(this.tree[0].children[0].children[i-2].children[0].children)
            console.log(this.tree[0].children[0].children[i-2].children[1].label)
            for (let j = 0; j < this.numArr3.length; j++) {
              if (this.tree[0].children[0].children[i-2].label == this.numArr3[j].sectionName){
                if (this.numArr3[j].type == 1){
                  this.tree[0].children[0].children[i-2].children[0].children.push({label: this.numArr3[j].knowName})
                }else {
                  this.tree[0].children[0].children[i-2].children[1].children.push({label: this.numArr3[j].knowName})
                }
              }
            }
          }
          console.log('arr3',this.numArr3)
        }else {
          this.$message.error('查看失败/查看出错！！')
          this.loading = false
        }
        this.querySection = queryData.data.section
        this.queryInfo = queryData.data
        console.log(this.queryInfo)
      },
      /*表格全选方法*/
      handleSelectionChange(val) {
        /*this.multipleSelection = val;*/
        console.log(val)
      },
      handleNodeClick(data) {
        console.log(data)
      },
      handleSizeChange(val) {
        this.limit = val
        this.queryTeach()
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val
        console.log('回收站',this.delstatus)
        console.log('主页',this.selstatus)
        if(this.delstatus == 1){
          this.delList()
        }else {
          this.queryTeach()
        }
        console.log(`当前页: ${val}`);
      }
    },
    computed:{
      TimeDate:{
        get(){
          if (this.addData.public_date!=null){
            let delayTime = new Date(this.addData.public_date).toJSON().split('T')[0];
            return delayTime
          }else {
            return null
          }
        }
      },
      sectionName:{
        get(){
          this.tree2[0].label = this.addData.section[0].section_name
          this.tree2[0].children[0].label = this.addData.section[0].child[0].section_name
          this.tree2[0].children[0].children[0].label = this.addData.section[0].child[0].child[0].section_name
          /*this.tree2[0].children[0].children[1].label = this.addData.section[0].child[0].child[1].section_name*/
        }
      }/*,
      sectionName2:{
        get(){
          this.tree2[0].children[0].label = this.addData.section[0].child[0].section_name
          return this.addData.section[0].child[0].section_name
        }
      },
      sectionName3:{
        get(){
          this.tree2[0].children[0].children[0].label = this.addData.section[0].child[0].child[0].section_name
          return this.addData.section[0].child[0].child[0].section_name
        }
      },
      sectionName4:{
        get(){
          this.tree2[0].children[0].children[1].label = this.addData.section[0].child[0].child[1].section_name
          return this.addData.section[0].child[0].child[1].section_name
        }
      }*/
    },
    created() {
      this.queryTeach()
    }
  }
</script>

<style scoped>
  .box {
    width: 100%;
    display: flex;
    flex-direction: row;
    /*//两侧对齐*/
    justify-content: space-between;
    /*//自动换行*/
    flex-wrap: wrap;
    /*//清除上下行的中间空隙*/
    align-content: flex-start;
  }

  .box_content {
    flex: 30 200px;
  }

  .box_content {
    flex: 30 150px;
  }

  /*//解决最后一行的问题*/
  .box:after {
    content: '';
    width: 50%;
  }

  .btn {
    margin-bottom: 15px;
  }

  .titles {
    text-align: right;
    margin-bottom: 10px;
  }

  .titles2 {
    margin-left: 20px;
    margin-bottom: 10px;
  }

  .bott {
    margin-bottom: 22px;
  }

  .queryTitle{
    margin-bottom: 5px;
  }
</style>
